
package com.ubao.network;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.URL;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;

import org.apache.http.HttpHost;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.ubao.activity.MyApp;

public class Connector {
    public static final int READ = 1;

    public static final int WRITE = 2;

    public static final int READ_WRITE = 3;

    public static HttpConnection open(String name, int mode,int timeouts) {

        HttpURLConnection conn = null;
        URL url = null;
        try {
            url = new URL(name);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        conn = openConnection(url);
        conn.setDoInput(true);
        conn.setDoOutput(true);
        
        conn.setConnectTimeout(timeouts);

        // conn.setReadTimeout(100000);// 2010-11-09 added by wyp

        HttpConnection co = new HttpConnection();
        co.setConnEx(conn);

        return co;
    }

    public static String NetType(Context context) {
        try {
            ConnectivityManager cm = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo info = cm.getActiveNetworkInfo();
            if (null == info)
                return null;
            String typeName = info.getTypeName().toLowerCase(); // WIFI/MOBILE
            if (typeName.equals("wifi")) {

            } else {
                typeName = info.getExtraInfo().toLowerCase();
                // 3gnet/3gwap/uninet/uniwap/cmnet/cmwap/ctnet/ctwap
            }
            // Log.v("out", "connect type:" + typeName);
            return typeName;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static HttpURLConnection openConnection(URL url) {
        final String netType = NetType(MyApp.getMyApp());
        if (netType != null
                && (netType.equalsIgnoreCase("cmwap") || netType.equalsIgnoreCase("uniwap"))) {
            java.net.Proxy p = new java.net.Proxy(java.net.Proxy.Type.HTTP, new InetSocketAddress(
                    android.net.Proxy.getDefaultHost(), android.net.Proxy.getDefaultPort()));
            try {
            	if (url.getProtocol().toLowerCase().equals("https")){
            		HttpsUtil.trustAllHosts();
            		HttpsURLConnection http = (HttpsURLConnection) url.openConnection(p);
    				((HttpsURLConnection) http).setHostnameVerifier(new HostnameVerifier() {
    					public boolean verify(String hostname, SSLSession session) {
    						return true;
    					}
    				});
    				return http;
            	}else{
            		return (HttpURLConnection) url.openConnection(p);
            	}
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
            }
        } else {
            try {
            	if (url.getProtocol().toLowerCase().equals("https")){
            		HttpsUtil.trustAllHosts();
            		HttpsURLConnection http = (HttpsURLConnection) url.openConnection();
    				((HttpsURLConnection) http).setHostnameVerifier(new HostnameVerifier() {
    					public boolean verify(String hostname, SSLSession session) {
    						return true;
    					}
    				});
    				return http;
            	}else{
                return (HttpURLConnection) url.openConnection();
            	}
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
            }
        }
    }

    public static HttpHost getProxy() {
        String iNetType = NetType(MyApp.getMyApp());
        if (null != iNetType) {
            if (iNetType.equalsIgnoreCase("cmwap")
                    || NetType(MyApp.getMyApp()).equalsIgnoreCase("uniwap")) {
                return new HttpHost(android.net.Proxy.getDefaultHost(),
                        android.net.Proxy.getDefaultPort(), "http");
            }
        }
        return null;
    }
}
